package com.hunttown.mes.impl;

import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.mes.rpc.api.LbsTerminalStatisticsRpcService;
import com.hunttown.mes.rpc.domain.LbsTerminalStatisticsDTO;
import com.hunttown.mes.rpc.domain.query.LbsTerminalStatisticsDTOQuery;
import com.hunttown.mes.service.LbsTerminalStatisticsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hunttown.mes.convert.LbsTerminalStatisticsConverts.convert;

/**
 * created by wangjunfu on 2022-01-17 10:16:55
 */
@Service
public class LbsTerminalStatisticsRpcServiceImpl implements LbsTerminalStatisticsRpcService {

    private final Logger logger = LoggerFactory.getLogger(LbsTerminalStatisticsRpcServiceImpl.class);

    private final LbsTerminalStatisticsService lbsTerminalStatisticsService;

    @Autowired
    public LbsTerminalStatisticsRpcServiceImpl(LbsTerminalStatisticsService lbsTerminalStatisticsService) {
        this.lbsTerminalStatisticsService = lbsTerminalStatisticsService;
    }

    @Override
    public LbsTerminalStatisticsDTO insert(LbsTerminalStatisticsDTO lbsTerminalStatisticsDTO) {
        try {
            return convert(lbsTerminalStatisticsService.insert(convert(lbsTerminalStatisticsDTO)));
        } catch (Exception e) {
            logger.error("LbsTerminalStatistics create failed.", e);
            throw new RuntimeException("LbsTerminalStatistics create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(LbsTerminalStatisticsDTO lbsTerminalStatisticsDTO) {
        try {
            return lbsTerminalStatisticsService.updateInfoById(convert(lbsTerminalStatisticsDTO));
        } catch (Exception e) {
            logger.error("LbsTerminalStatistics update by id failed.", e);
            throw new RuntimeException("LbsTerminalStatistics update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(LbsTerminalStatisticsDTOQuery lbsTerminalStatisticsDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (lbsTerminalStatisticsDTOQuery.getW_id() != null && lbsTerminalStatisticsDTOQuery.getW_id() > 0) {
                query.put("w_id", lbsTerminalStatisticsDTOQuery.getW_id());
            }

            if (lbsTerminalStatisticsDTOQuery.getIds() != null && !lbsTerminalStatisticsDTOQuery.getIds().equals("")) {
                query.put("ids", lbsTerminalStatisticsDTOQuery.getIds());
            }

            if (lbsTerminalStatisticsDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", lbsTerminalStatisticsDTOQuery.getStopFlag());
            }

            if (lbsTerminalStatisticsDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", lbsTerminalStatisticsDTOQuery.getDeleteFlag());
            }

            return lbsTerminalStatisticsService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("LbsTerminalStatistics update by query failed.", e);
            throw new RuntimeException("LbsTerminalStatistics update by query failed, Please order your code.", e);
        }
    }

    @Override
    public LbsTerminalStatisticsDTO selectById(Integer id) {
        try {
            return convert(lbsTerminalStatisticsService.selectById(id));
        } catch (Exception e) {
            logger.error("LbsTerminalStatistics select by id failed.", e);
            throw new RuntimeException("LbsTerminalStatistics select by id failed, Please order your code.", e);
        }
    }

    @Override
    public LbsTerminalStatisticsDTO selectObjByQuery(LbsTerminalStatisticsDTOQuery lbsTerminalStatisticsDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (lbsTerminalStatisticsDTOQuery.getEntId() != null) {
                query.put("entId", lbsTerminalStatisticsDTOQuery.getEntId());
            }

            if (lbsTerminalStatisticsDTOQuery.getCarId() != null) {
                query.put("carId", lbsTerminalStatisticsDTOQuery.getCarId());
            }

            if (lbsTerminalStatisticsDTOQuery.getStaticType() != null) {
                query.put("staticType", lbsTerminalStatisticsDTOQuery.getStaticType());
            }

            if (lbsTerminalStatisticsDTOQuery.getTerminalId() != null) {
                query.put("terminalId", lbsTerminalStatisticsDTOQuery.getTerminalId());
            }

            if (StringUtils.isNotBlank(lbsTerminalStatisticsDTOQuery.getShowDate())) {
                query.put("showDate", lbsTerminalStatisticsDTOQuery.getShowDate());
            }

            if (lbsTerminalStatisticsDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", lbsTerminalStatisticsDTOQuery.getStopFlag());
            }

            if (lbsTerminalStatisticsDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", lbsTerminalStatisticsDTOQuery.getDeleteFlag());
            }

            if (StringUtils.isNotBlank(lbsTerminalStatisticsDTOQuery.getOrderBy())) {
                query.put("orderBy", lbsTerminalStatisticsDTOQuery.getOrderBy());
            }

            return convert(lbsTerminalStatisticsService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("LbsTerminalStatistics select obj by query failed.", e);
            throw new RuntimeException("LbsTerminalStatistics select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<LbsTerminalStatisticsDTO> getListForPage(LbsTerminalStatisticsDTOQuery lbsTerminalStatisticsDTOQuery) {
        Query query = Query.page(lbsTerminalStatisticsDTOQuery.getPn(), lbsTerminalStatisticsDTOQuery.getPageSize());

        if (lbsTerminalStatisticsDTOQuery.getId() != null && lbsTerminalStatisticsDTOQuery.getId() > 0) {
            query.put("id", lbsTerminalStatisticsDTOQuery.getId());
        }

        if (lbsTerminalStatisticsDTOQuery.getIds() != null && !lbsTerminalStatisticsDTOQuery.getIds().equals("")) {
            query.put("ids", lbsTerminalStatisticsDTOQuery.getIds());
        }

        if (StringUtils.isNotBlank(lbsTerminalStatisticsDTOQuery.getLikeName())) {
            query.put("likeName", lbsTerminalStatisticsDTOQuery.getLikeName());
        }

        if (StringUtils.isNotBlank(lbsTerminalStatisticsDTOQuery.getShowDate())) {
            query.put("showDate", lbsTerminalStatisticsDTOQuery.getShowDate());
        }

        if (StringUtils.isNotBlank(lbsTerminalStatisticsDTOQuery.getStartDate())) {
            query.put("startDate", lbsTerminalStatisticsDTOQuery.getStartDate());
        }

        if (StringUtils.isNotBlank(lbsTerminalStatisticsDTOQuery.getEndDate())) {
            query.put("endDate", lbsTerminalStatisticsDTOQuery.getEndDate());
        }

        if (lbsTerminalStatisticsDTOQuery.getStopFlag() != null) {
            query.put("stopFlag", lbsTerminalStatisticsDTOQuery.getStopFlag());
        }

        if (lbsTerminalStatisticsDTOQuery.getDeleteFlag() != null) {
            query.put("deleteFlag", lbsTerminalStatisticsDTOQuery.getDeleteFlag());
        }

        if (StringUtils.isNotBlank(lbsTerminalStatisticsDTOQuery.getOrderBy())) {
            query.put("orderBy", lbsTerminalStatisticsDTOQuery.getOrderBy());
        }

        List<LbsTerminalStatisticsDTO> list = convert(lbsTerminalStatisticsService.selectByQuery(query));
        Page<LbsTerminalStatisticsDTO> page = Page.from(lbsTerminalStatisticsDTOQuery.getPn(), lbsTerminalStatisticsDTOQuery.getPageSize(), list);
        page.setTotalItems(lbsTerminalStatisticsService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return lbsTerminalStatisticsService.deleteById(id);
        } catch (Exception e) {
            logger.error("LbsTerminalStatistics delete failed.", e);
            throw new RuntimeException("LbsTerminalStatistics delete failed, Please order your code.", e);
        }
    }
}